package com.zx.map.controller;

import com.zx.map.service.AgentInfoService;
import com.zx.map.service.CreditRepayLogService;
import com.zx.map.service.MerchantInfoService;
import com.zx.map.service.domain.CreditRepayLogDownloadDomain;
import com.zx.map.service.model.AgentInfoModel;
import com.zx.map.service.model.CreditRepayLogModel;
import com.zx.map.service.model.MerchantInfoModel;
import com.zx.mobile.domain.CreditRepayLogQueryDomain;
import com.zx.platform.utils.PmpUtils;
import com.zx.platform.utils.report.ReportUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

@Controller
@RequestMapping("/manage/creditRepayLog")
public class CreditRepayLogController {
    /**
     * log
     */
    private static Logger log = Logger.getLogger(CreditRepayLogController.class);

    @Autowired
    private CreditRepayLogService service;
    @Autowired
    private AgentInfoService agentInfoService;
    @Autowired
    private MerchantInfoService merchantInfoService;
    /**
     * 信用卡查询功能	 *
     *
     * @param domain
     * @param model
     * @param request
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("/creditRepayLogList")
    public String creditRepayLogList(CreditRepayLogQueryDomain domain, Model model,
                             HttpServletRequest request) {

        try {// 初始化信息
            Integer pageNum = domain.getPageNum();
            Integer pageSize = domain.getPageSize();
            Integer pageCount = domain.getPageCount();
            if (null == pageNum) {
                pageNum = 1;
            }
            if (null == pageSize) {
                pageSize = 10;
            }
            if (StringUtils.isBlank(domain.getBeginDate())) {
                domain.setBeginDate(PmpUtils.dateToString(new Date(), "yyyy-MM-dd"));
            }
            if (StringUtils.isBlank(domain.getEndDate())) {
                domain.setEndDate(PmpUtils.dateToString(new Date(),
                        "yyyy-MM-dd"));
            }
            Map map = service.getPageByDomain(pageSize, pageNum, domain);
            List<CreditRepayLogModel> creditRepayInfoList = (List<CreditRepayLogModel>) map.get("list");
            int recordCount = Integer.valueOf(map.get("count").toString());
            // 页面信息
            if (pageSize * pageNum > recordCount + pageSize - 1) {
                pageNum = 1;
            }
            model.addAttribute("list", creditRepayInfoList);
            model.addAttribute("pageNum", pageNum);
            model.addAttribute("pageSize", pageSize);
            model.addAttribute("pageCount",
                    PmpUtils.getPageCount(recordCount, pageSize));
            model.addAttribute("recordCount", recordCount);
            model.addAttribute("model", domain);
            return "/jsp/map/creditRepayLog/creditRepayLogList";
        } catch (Exception e) {
            log.error("内部异常", e);
            model.addAttribute("errorMsg", "出错了");
            return "/jsp/platform/error";
        }
    }
    /**
     * 取现下载	 *
     *
     * @param domain
     * @param model
     * @param request
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("/downLncashList")
    public String downLncashList(CreditRepayLogQueryDomain domain, Model model,
                                 HttpServletRequest request, HttpServletResponse response) {

        try {
            if (StringUtils.isBlank(domain.getBeginDate())) {
                domain.setBeginDate(PmpUtils.dateToString(new Date(), "yyyy-MM-dd"));
            }
            if (StringUtils.isBlank(domain.getEndDate())) {
                domain.setEndDate(PmpUtils.dateToString(new Date(),
                        "yyyy-MM-dd"));
            }
            List<CreditRepayLogModel> creditRepayInfoList = service.getListByDomain(domain);

            List<CreditRepayLogDownloadDomain> reportList = new ArrayList<CreditRepayLogDownloadDomain>();
            for (CreditRepayLogModel o : creditRepayInfoList) {
                
                CreditRepayLogDownloadDomain temp = new CreditRepayLogDownloadDomain();
                BeanUtils.copyProperties(o,temp);
                //设置订单类型  商户名称 代理商名称
                MerchantInfoModel tempMer = merchantInfoService.getModelByKey(o.getMerchantId());
                AgentInfoModel tempAgent = agentInfoService.getAgentInfoModelBySysAccount(o.getAgentSysAccount());
                temp.setAgentSysName(tempAgent.getAgentName());
                temp.setMerSysName(tempMer.getMerchantName());
                reportList.add(temp);
                if("N".equals(temp.getRepayStatus())){
                	temp.setRepayStatus("初始");
                }else if("S".equals(temp.getRepayStatus())){
                	temp.setRepayStatus("成功");
                }else if("F".equals(temp.getRepayStatus())){
                	temp.setRepayStatus("失败");
                }
            }
            Map<Integer, Object> formatAttr = new HashMap<Integer, Object>();
            formatAttr.put(1, ReportUtil.Date_cro);
            formatAttr.put(3, ReportUtil.Decimal_Money_chn);
            formatAttr.put(4, ReportUtil.Decimal_Money_chn);
            formatAttr.put(11, ReportUtil.Decimal_Money_chn);
            try {

                String[] header = null;
                header = new String[]{"创建日期", "还款订单号", "还款金额","交易金额","商户账户","商户名称","代理商账户","代理商名称","还款状态",
                        "结算流水号","结算金额","结算状态 ","结算备注"};
                String sts = ReportUtil.exportReport(header, reportList, null,
                        "信用卡还款查询", formatAttr, null, response);
                String[] st = sts.split("#");
                ReportUtil.download(response, st[0], st[1]);
            } catch (Exception e) {
                log.error("信用卡还款查询", e);
            }
        } catch (Exception e) {
            log.error("内部异常", e);
            model.addAttribute("errorMsg", "出错了");
            return "/jsp/platform/error";
        }
        return null;
    }
}
